package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JFormattedTextField;

import java.text.Format;

import javax.swing.JComboBox;
import javax.swing.JButton;

import dto.AdminDto;
import dto.StudentDto;
import dto.TeacherDto;
import bus.UpdateUserBus;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Window.Type;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultComboBoxModel;

public class JFrameAdminChangePassword extends JFrame {

	private JPanel contentPane;
	private JTextField txtUserId;
	private JTextField txtOldPass;
	private JTextField txtNewPass;
	private JComboBox cboRole;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrameAdminChangePassword frame = new JFrameAdminChangePassword();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFrameAdminChangePassword() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				JFrameAdminMainPage jFrameAdminMainPage = new JFrameAdminMainPage();
				jFrameAdminMainPage.setVisible(true);
			}
		});
		setType(Type.POPUP);
		setResizable(false);
		setTitle("Admin - Change user 's password");
		setBounds(100, 100, 459, 316);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("Enter new user's detail:");
		label.setFont(new Font("Tahoma", Font.BOLD, 12));
		label.setBounds(102, 54, 278, 14);
		contentPane.add(label);

		JLabel label_1 = new JLabel("User ID:");
		label_1.setBounds(102, 108, 90, 14);
		contentPane.add(label_1);

		txtUserId = new JTextField();
		txtUserId.setColumns(10);
		txtUserId.setBounds(192, 105, 137, 20);
		contentPane.add(txtUserId);

		txtOldPass = new JTextField();
		txtOldPass.setColumns(10);
		txtOldPass.setBounds(192, 133, 137, 20);
		contentPane.add(txtOldPass);

		JLabel lblOldPassword = new JLabel("Old password:");
		lblOldPassword.setBounds(102, 136, 90, 14);
		contentPane.add(lblOldPassword);

		JLabel label_6 = new JLabel("Role:");
		label_6.setBounds(102, 80, 90, 14);
		contentPane.add(label_6);

		cboRole = new JComboBox();
		cboRole.setModel(new DefaultComboBoxModel(new String[] {"Student", "Teacher", "Admin"}));
		cboRole.setBounds(192, 77, 137, 20);
		contentPane.add(cboRole);

		JButton btnChange = new JButton("Change");
		btnChange.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (checkDataInput()) {
					String useId = txtUserId.getText();
					String oldPass = txtOldPass.getText();
					String newPass = txtNewPass.getText();
					String role = (String) cboRole.getSelectedItem();

					UpdateUserBus updateUserBus = new UpdateUserBus();
					String respone = "";

					switch (role) {
					case "Student":

						StudentDto student = new StudentDto(useId, oldPass, -1);
						respone = updateUserBus.changePasswordStudent(student,
								newPass);
						showMessage(respone);
						break;
					case "Teacher":
						TeacherDto teacher = new TeacherDto(useId, oldPass, -1);
						respone = updateUserBus.changePasswordTeacher(teacher,
								newPass);
						showMessage(respone);
						break;

					case "Admin":
						AdminDto admin = new AdminDto(useId, oldPass, -1);
						respone = updateUserBus.changePasswordAdmin(admin,
								newPass);
						showMessage(respone);
						break;
					default:
						break;
					}
				}
			}
		});
		btnChange.setBounds(240, 213, 89, 23);
		contentPane.add(btnChange);

		txtNewPass = new JTextField();
		txtNewPass.setColumns(10);
		txtNewPass.setBounds(192, 162, 137, 20);
		contentPane.add(txtNewPass);

		JLabel lblNewPassword = new JLabel("New password:");
		lblNewPassword.setBounds(102, 165, 90, 14);
		contentPane.add(lblNewPassword);
	}

	private void showMessage(String mess) {
		JOptionPane.showMessageDialog(this, mess);
	}

	private boolean checkDataInput() {

		if (txtUserId.getText().equals("")) {
			showMessage("Please enter user ID!");
			return false;
		}
		if (txtOldPass.getText().equals("")) {
			showMessage("Please enter old password!");
			return false;
		}
		if (txtNewPass.getText().equals("")) {
			showMessage("Please enter new password!");
			return false;
		}
		if (txtNewPass.getText().equals(txtOldPass.getText())) {
			showMessage("Please enter different new password!");
			return false;
		}

		return true;
	}

}
